package com.lanou3g.ptp.task.job;

import com.lanou3g.ptp.entity.Task;
import com.lanou3g.ptp.task.service.TaskService;
import com.lanou3g.ptp.utils.DateUtils;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class TaskJob extends QuartzJobBean {
    @Autowired
    private TaskService taskService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        List<Task> notStartTasks = taskService.findByStatus(Task.STATUS_NOT_START);
        for (Task task : notStartTasks) {
            Date startTime = task.getTaskStartTime();
            if (DateUtils.isToday(startTime)) {
                taskService.startTask(task.getId());
            }
        }
        List<Task> runningTasks = taskService.findByStatus(Task.STATUS_RUNNING);
        for (Task task : runningTasks) {
            Date endTime = task.getTaskEndTime();
            if (DateUtils.isYesterday(endTime)) {
                taskService.endTask(task.getId());
            }
        }
    }
}
